package com.jxaummd.mushroom.main.common.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.jxaummd.mushroom.R;
import com.jxaummd.mushroom.R2;
import com.jxaummd.mushroom.core.base.fragment.PresenterFragment;
import com.jxaummd.mushroom.main.common.contract.SignUpContract;
import com.jxaummd.mushroom.main.common.presenter.SignUpPresenter;


import butterknife.BindView;
import butterknife.OnClick;

public class SignUpFragment extends PresenterFragment<SignUpContract.Presenter>
        implements SignUpContract.View {

    private String phoneNumber = "18256705494";

    @BindView(R2.id.ec_et_common_signup_testcode)
    EditText mEditTestCode = null;

    @BindView(R2.id.ec_et_common_signup_password)
    EditText mEditPassword = null;

    @BindView(R2.id.ec_tv_common_signup_hint)
    TextView mHintText = null;

    @BindView(R2.id.ec_tv_common_signup_time)
    TextView mTimer = null;


    @OnClick(R2.id.ec_btn_common_signup_submit)
    public void submit() {
        String password = mEditPassword.getText().toString();
        String testcode = mEditTestCode.getText().toString();
        String hintString = "";
        if (password.length() < 6) {
            hintString = "密码的长度要大于6位哦！";
        }
        if (TextUtils.isEmpty(testcode)) {
            hintString = "验证码不能为空哦！";
        }
        if (hintString.length() <= 1)
            mPresenter.register(phoneNumber, password, testcode);
        else
            ToastUtils.showShort(hintString);
    }

    @OnClick(R2.id.ec_ib_common_signup_close)
    public void close() {
        pop();
    }


    public static SignUpFragment newInstance(String phoneNumber) {
        SignUpFragment fragment = new SignUpFragment();
        fragment.phoneNumber = phoneNumber;
        return fragment;
    }

    @Override
    protected SignUpContract.Presenter initPresenter() {
        return new SignUpPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.common_signup;
    }

    @Override
    public void onNewBundle(Bundle args) {
        super.onNewBundle(args);
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mHintText.append(phoneNumber);
        mPresenter.start();
    }

    @Override
    public void updateTime(String message) {
        post(() -> {
            mTimer.setText(message);
        });
    }

    @Override
    public void registerSuccess() {
        pop();
    }
}
